<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/9/15
 * Time: 14:46
 */

namespace app\admin\controller;

use app\admin\service\AuthService;
use app\admin\validator\LoginValidator;
use app\service\CaptchaService;
use lib\annotations\AdminPermission;
use lib\common\CommonController;
use lib\annotations\Permission;

class Auth extends CommonController
{
    /**
     * 图片验证码
     * @param CaptchaService $service
     * @return void
     */
    public function captcha (CaptchaService $service): void {
        $data = $service->generate();
        $this->success($data);
    }

    /**
     * 账号密码登录
     * @param AuthService $service
     * @return void
     */
    public function login (AuthService $service): void {
        $params = $this->request->post();
        (new LoginValidator())->goCheck($params);
        $data = $service->login($params);
        $this->success($data);
    }

    /**
     * 刷新token
     * @param AuthService $service
     * @return void
     */
    public function refreshToken (AuthService $service): void {
        $data = $service->refreshToken();
        $this->success($data);
    }


    /**
     * 如果PHP8可使用 #[AdminPermission("/admin/auth/userinfo")] 写法
     * @Permission
     * 登录后获取用户信息及有权限的菜单
     * @param AuthService $service
     * @return void
     */
    #[AdminPermission]
    public function userinfo (AuthService $service): void {
        $user = $this->request->adminUser();
        $data = $service->userinfo($user);
        $this->success($data);
    }
}
